Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    openid: "",
    sex: ['女', '男'],
    arr: [],
    array: [],
    order: []
  },

  /**
    *买家入口
    */
  goBuy: function () {
    var that = this;
    wx.getUserInfo({
      success: function (res) {
        var userInfo = res.userInfo;
        that.setData({
          userInfo: userInfo,
        })
        console.log(userInfo);
        var openid = wx.getStorageSync("openid");
        that.setData({
          openid: openid
        })
        wx.request({
          url: 'https://www.wenzhixinxi.com/phptest/judge.php', //仅为示例，并非真实的接口地址
          data: {
            openid: that.data.openid
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            console.log(res.data);
            if (res.data.jsonObj == null) {
              console.log("执行添加用户")

              wx.request({
                url: 'https://www.wenzhixinxi.com/phptest/insertuser.php', //仅为示例，并非真实的接口地址
                data: {
                  userwxname: that.data.openid,
                  username: userInfo.nickName,
                  usersex: that.data.sex[userInfo.gender],
                  userpic: userInfo.avatarUrl,
                },
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                  console.log(that.data.sex[userInfo.gender]);
                  console.log(that.data.openid);
                  console.log(res.data)
                  console.log("添加成功，现在再执行查询")

                }
              })

            }

            wx.request({
              url: 'https://www.wenzhixinxi.com/phptest/searchcare.php', //仅为示例，并非真实的接口地址
              data: {
                userwxname: that.data.openid,
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success: function (res) {
                console.log(res.data)
                console.log("添加成功，现在再执行查询")
                that.setData({
                  arr: res.data.jsonObj
                })
                wx.setStorageSync("arr", that.data.arr);
              }
            })


          }
        })
        wx.navigateTo({
          url: '../index/search'
        })
      }
    })

  },
  goSale: function () {
    var that = this;
    wx.getUserInfo({
      success: function (res) {
        var userInfo = res.userInfo;
        that.setData({
          userInfo: userInfo,
        })
        console.log(userInfo);
        var openid = wx.getStorageSync("openid");
        that.setData({
          openid: openid
        })
        console.log(that.data.openid);
        wx.request({
          url: 'https://www.wenzhixinxi.com/phptest/judgeshangjia.php', //仅为示例，并非真实的接口地址
          data: {
            openid: that.data.openid
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            console.log(res.data);
            if (res.data.jsonObj == null) {
              console.log("执行添加用户")

              wx.request({
                url: 'https://www.wenzhixinxi.com/phptest/insertshangjia.php', //仅为示例，并非真实的接口地址
                data: {
                  userwxname: that.data.openid,
                  username: userInfo.nickName,
                  usersex: that.data.sex[userInfo.gender],
                  userpic: userInfo.avatarUrl,
                },
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                  console.log(that.data.sex[userInfo.gender]);
                  console.log(that.data.openid);
                  console.log(res.data)
                  console.log("添加成功，现在再执行查询")

                }
              })

            }
            wx.request({
              url: 'https://www.wenzhixinxi.com/phptest/judgeshop.php', //仅为示例，并非真实的接口地址
              data: {
                openid: that.data.openid
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success: function (res) {
                console.log(res.data.jsonObj)
                console.log("添加成功，现在再执行查询")
                if (res.data.jsonObj == null) {
                  wx.showModal({
                    title: '提示',
                    content: '请先建立店铺',
                    showCancel: false,
                    success: function (res) {
                      if (res.confirm) {
                        console.log('用户点击确定')
                      }
                    }
                  })
                }
                else {
                  var shop_id = res.data.jsonObj[0].id;
                  wx.setStorageSync("shopid", res.data.jsonObj[0].id);
                  // wx.request({
                  //   url: 'https://www.wenzhixinxi.com/phptest/idsearchgoods.php', //仅为示例，并非真实的接口地址
                  //   data: {
                  //     id: shop_id
                  //   },
                  //   header: {
                  //     'content-type': 'application/json' // 默认值
                  //   },
                  //   success: function (res) {
                  //     console.log(res.data.jsonObj);
                  //     that.setData({
                  //       goods: res.data.jsonObj
                  //     })
                  //     wx.setStorageSync("goods", that.data.goods);
                  //     console.log("添加成功，现在再执行查询")

                  //   }
                  // })
                  wx.request({
                    url: 'https://www.wenzhixinxi.com/phptest/idsearchorders.php', //仅为示例，并非真实的接口地址
                    data: {
                      id: shop_id
                    },
                    header: {
                      'content-type': 'application/json' // 默认值
                    },
                    success: function (res) {
                      console.log("订单")
                      console.log(res.data.jsonObj);
                      that.setData({
                        order: res.data.jsonObj
                      })
                      wx.setStorageSync("order", that.data.order);
                    }
                  })
                  that.setData({
                    array: res.data.jsonObj

                  })
                  wx.setStorageSync("goods", that.data.array);

                }
              }
            })

          }
        })
      }
    })
    wx.switchTab({
      url: '../sell_goods/sell_goods'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
